﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using AccessProvider;

namespace Interface.Lop
{
    public partial class DanhSachLop : Form
    {
        DataConnection dc = new DataConnection();
        
        public DanhSachLop()
        {
            InitializeComponent();
        }

        private void DanhSachLop_Load(object sender, EventArgs e)
        
        {
            loadLop();                                    
        }

        protected void loadDSHS()
        {
            int rows = lvHS.Items.Count;
            if (rows > 0)
            {
                for (int i = 0; i < rows; i++)
                {
                    ListViewItem item = lvHS.Items[0];
                    lvHS.Items.Remove(item);
                }
            }
            string tenlop = cbLop.SelectedItem.ToString();
            string sql = "select * from HOCSINH hs, Lop l where hs.MaLop = l.MaLop and l.TenLop = '" + tenlop + "'"; 
            DataTable dt = dc.getData(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                string mahs = row["MaHS"].ToString();
                string hoten = row["HoTen"].ToString();
                Boolean gioitinh = Convert.ToBoolean(row["GioiTinh"]);
                string ngaysinh = row["NgaySinh"].ToString();
                string malop = row["MaLop"].ToString();
                string email = row["Email"].ToString();
                string dienthoai = row["DienThoai"].ToString();
                string diachi = row["DiaChi"].ToString();

                ClassLib.HocSinh entity = new ClassLib.HocSinh(mahs, gioitinh, hoten, malop, ngaysinh, email, dienthoai, diachi);

                ListViewItem item1 = new ListViewItem(mahs.ToString());                                
                item1.SubItems.Add(hoten);
                string gt = null;
                if (gioitinh == true)
                {
                    gt = "nam";
                }
                else gt = "nữ";

                item1.SubItems.Add(gt);
                item1.SubItems.Add(ngaysinh);
                item1.SubItems.Add(diachi);

                item1.Tag = entity;
                lvHS.Items.Add(item1);               
            }            
        }

        private void loadLop()
        {            
            string sqlLop = "select * from Lop";            
            DataTable dt = dc.getData(sqlLop);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    string tenlop = row["TenLop"].ToString();
                    cbLop.Items.Add(tenlop);
                }
            }
           
        }

        private void cbLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbLop.SelectedItem != null)
            {
                string tenlop = cbLop.SelectedItem.ToString();
                string sql = "select * from HOCSINH hs, Lop l where hs.MaLop = l.MaLop and l.TenLop = '" + tenlop + "'";

                DataTable dt = dc.getData(sql);
                //string siso = dc.checksiso(Malop).ToString();
                lbss.Text = dt.Rows.Count.ToString();
                //string sqlLop = "select SiSo from LOP where MaLop = '"+Malop+"' " ;            
                loadDSHS();
            }
        }

        

        
    }
}
